package com.rrx.mapper;

import com.rrx.model.Menu;
import org.apache.ibatis.annotations.*;

@Mapper
public interface MenuMapper {

    /**
     * 通过名字查看菜单是否存在
     * @param name 菜单名
     * @return 查询到的数量
     */
    @Select("select count(id) from menu where menu_name=#{name}")
    public int isMenuExists(@Param("name") String name);

    /**
     * 添加菜单
     * @param name 菜单名
     * @param permission 菜单权限
     * @param order 菜单优先级
     * @param request 菜单请求
     * @return 执行数
     */
    @Insert("insert into menu (menu_name, menu_permission, menu_order, request, parent) values(#{name}, #{permission}, #{order}, #{request}, #{parent})")
    public int addMenu(@Param("name") String name, @Param("permission") String permission, @Param("order") int order, @Param("request") String request, @Param("parent") Integer id);

    @Select("select * from menu where parent is null order by menu_order desc")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "menu_name", property = "name"),
            @Result(column = "menu_permission", property = "menuPermission"),
            @Result(column = "request", property = "request"),
            @Result(column = "parent", property = "parent")
    })
    public Menu[] getAllFirstMenu();

    @Select("select id, menu_name,menu_permission,request from menu where parent = #{parentId} and state = 0 order by menu_order desc")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "menu_name", property = "name"),
            @Result(column = "menu_permission", property = "menuPermission"),
            @Result(column = "request", property = "request")
    })
    public Menu[] getSecondMenuById(@Param("parentId") int id);

    @Select("select * from menu where parent is null and id = #{id}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "menu_name", property = "name"),
            @Result(column = "menu_permission", property = "menuPermission"),
            @Result(column = "request", property = "request"),
            @Result(column = "parent", property = "parent")
    })
    public Menu getFirstMenuById(@Param("id") int id);

    @Select("select id,menu_name_menu_permission,request,parent from menu where menu_permission=#{permission}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "menu_name", property = "name"),
            @Result(column = "menu_permission", property = "menuPermission"),
            @Result(column = "request", property = "request"),
            @Result(column = "parent", property = "parent")
    })
    public Menu getMenuByPermission(@Param("permission") String permission);


    @Select("select m.* from menu m,(select distinct(rap.permission) as perm from role_and_permission rap, (select roleid as rid from admin_and_role where adminid=#{id}) r where rap.roleid=r.rid) p where p.perm=m.menu_permission and m.parent > 0 and m.state = 0 order by menu_order desc")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "menu_name", property = "name"),
            @Result(column = "menu_permission", property = "menuPermission"),
            @Result(column = "request", property = "request"),
            @Result(column = "parent", property = "parent")
    })
    public Menu[] getAdminMenusById(@Param("id") int id);
}
